package com.kehao.leetcode.middle.sort_search;

import org.junit.Test;

public class SearchRangeSolution {
    public int[] searchRange(int[] nums, int target) {
        if(nums.length==0) return new int[]{-1, -1};
        int left = 0,right = nums.length-1;
        int mid = (left+right)/2;
        while (left<right){
            if(nums[mid]<target){
                left = mid+1;
            }else if(nums[mid]>target){
                right = mid-1;
            }else {
                break;
            }
            mid = (left+right)/2;
        }
        if(nums[mid]==target){
            left = mid;
            right = mid;
            while (left>=0&&nums[left]==target) left--;
            left++;
            while (right<nums.length&&nums[right]==target) right++;
            right--;
            return new int[]{left,right};
        }else {
            return new int[]{-1, -1};
        }
    }

    @Test
    public void test01(){
        int[] nums = {5,7,7,8,8,10};
        int target = 8;
        int[] result = searchRange(nums, target);
        System.out.println(result[0]+"   "+result[1]);
    }

    @Test
    public void test02(){
        int[] nums = {5,7,7,8,8,10};
        int target = 6;
        int[] result = searchRange(nums, target);
        System.out.println(result[0]+"   "+result[1]);
    }

    @Test
    public void test03(){
        int[] nums = {1};
        int target = 1;
        int[] result = searchRange(nums, target);
        System.out.println(result[0]+"   "+result[1]);
    }
}
